﻿/* $RCSFile: HeaderLogo.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/WebControls/HeaderLogo.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.Core;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Helpers;
using AddonNice.Security;
using AddonNice.Configuration;

using Trace2    =   System.Diagnostics.Trace;
using Path2     =   AddonNice.Settings.Path;

namespace AddonNice.UI.WebControls
{

    /// <summary>
    /// Get the Logo from settings and display it
    /// Use either PageTitle or PageKeyPhrase for alternate text
    /// </summary>
    public class HeaderLogo : PlaceHolder
    {
        Image Img			            =	new Image();
		HtmlAnchor Anc	                =	new HtmlAnchor();
		string	_HRef		            =	"javascript:window.external.addfavorite('{0}', '{1}')";
		string	_Title		            =	"Add to Favorite";
		string	_Target		            =	string.Empty;
		string  _ImageUrl	            =	string.Empty;
		private bool _dataBindOnInit	=   true;

		

		public string ImageUrl
		{
			get
			{
				return _ImageUrl;
			}
			set
			{
				_ImageUrl	            =	value;
			}
		}
	
		public string HRef
		{
			get
			{
				return _HRef;
			}
			set
			{
				_HRef	                =	value;
			}
		}
		public string Title
		{
			get
			{
				return _Title;
			}
			set
			{
				_Title	                =	value;
			}
		}

        public string Target
		{
			get
			{
				return _Target;
			}
			set
			{
				_Target	                =	value;
			}
		}

        bool _UsePageTitle              =   true;
        public bool UsePageTitle
		{
			get
			{
				return _UsePageTitle;
			}
			set
			{
				_UsePageTitle           =	value;
			}
		}

        /// <summary>
		/// Should bind in OnInit
		/// </summary>
		[DefaultValue(true)]
		public bool DataBindOnInit
		{
			get
			{
				return _dataBindOnInit;
			}
			set
			{
				_dataBindOnInit     =   value;
			}
		}
		
		public HeaderLogo()
		{
			EnableViewState         =   false;
		}

		protected override void OnInit(EventArgs e)
		{
            if (WebControlsTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]HeaderLogo OnInit BEG DataBindOnInit: {0}, PortalSettings.IsUnderCRM: {1}",DataBindOnInit,PortalSettings.IsUnderCRM),WebControlsTraceSwitch.Sw.Info);
			if( DataBindOnInit )
			{
				DataBind();
			}
            if (WebControlsTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]HeaderLogo OnInit END.",WebControlsTraceSwitch.Sw.Info);
        }

        protected override void CreateChildControls()
        {
            Controls.Clear();
			HtmlTable Tb		        =	new HtmlTable();
			Tb.CellPadding		        =	0;
			Tb.CellSpacing		        =	0;
			HtmlTableRow rw		        =	new HtmlTableRow();
			HtmlTableCell c1	        =	new HtmlTableCell();

		    string str				    =	"Portal Title"; 
            if( !DesignMode )
            {
                // Obtain PortalSettings from Current Context
                PortalSettings portalSettings = (PortalSettings) HttpContext.Current.Items[StStr.strPortalSettings];

                //PortalImage
                if( !PortalSettings.IsUnderCRM &&
                    (( ImageUrl != string.Empty ) || portalSettings.CustomSettings[StStr.SiteSettingsLogo].Value != string.Empty) )
                {
					if ( ImageUrl == string.Empty )
                         Img.ImageUrl   =   Path2.WebPathCombine(portalSettings.DefaultImageVirtualPath, portalSettings.CustomSettings[StStr.SiteSettingsLogo].Value);
					else Img.ImageUrl	=	ImageUrl;
                    CultureInfo ci              =   portalSettings.PortalContentLanguage;
				    string currentLanguage      =   ci.Name.ToUpper().Replace('-', '_');
				    string currentNLanguage     =   string.Empty;
                    bool IsNeutralCulture       =   ci.IsNeutralCulture;
                    if ( !IsNeutralCulture )
                    {
                        ci                      =   ci.Parent;
			            currentNLanguage        =   ci.Name.ToUpper().Replace('-', '_');
                    }
                    string AltText              =   string.Empty;
                    if ( UsePageTitle )
                    {
                        if ( ( portalSettings.ActivePageSettings == null ) || !portalSettings.ActivePageSettings.Settings.StringValue(currentLanguage,ref AltText) )
                            if ( ( portalSettings.ActivePageSettings == null ) || !portalSettings.ActivePageSettings.Settings.StringValue(currentNLanguage,ref AltText) )
                                if ( ( portalSettings.ActivePageSettings == null ) || !portalSettings.ActivePageSettings.Settings.StringValue(StStr.PageTitle,ref AltText) )
                                    if ( !portalSettings.CustomSettings.StringValue(StStr.SiteSettingsPageTitle,ref AltText) )
                                        AltText = portalSettings.PortalTitle;
                    }
                    else
                    {
                        ci                      =   portalSettings.PortalContentLanguage;
				        currentLanguage         =   StStr.PageKeyPhrase_under+ci.Name.ToUpper().Replace('-', '_');
                        if ( !IsNeutralCulture )
                        {
                            ci                  =   ci.Parent;
			                currentNLanguage    =   StStr.PageKeyPhrase_under+ci.Name.ToUpper().Replace('-', '_');
                        }
                        if ( ( portalSettings.ActivePageSettings == null ) || !portalSettings.ActivePageSettings.Settings.StringValue(currentLanguage,ref AltText) )
                            if ( ( portalSettings.ActivePageSettings == null ) || !portalSettings.ActivePageSettings.Settings.StringValue(currentNLanguage,ref AltText) )
                                if ( ( portalSettings.ActivePageSettings == null ) || !portalSettings.ActivePageSettings.Settings.StringValue(StStr.PageTitle,ref AltText) )
                                    if ( !portalSettings.CustomSettings.StringValue(StStr.SiteSettingsPageTitle,ref AltText) )
                                        AltText = portalSettings.PortalTitle;
                    }
                    Img.AlternateText           =   AltText; 
					str		                    =	portalSettings.PortalTitle; 
				    Anc.HRef		            =	string.Format(HRef,Page.Request.Url.AbsoluteUri,str);
                }
                else // if no image url Img invisible
                {
                    Img.Visible                 =   false;
                }
            }
            if ( !PortalSettings.IsUnderCRM )
            {
			    Anc.Title				        =	Title;
			    Anc.Target				        =	Target;
			    Anc.Controls.Add(Img);

			    c1.Controls.Add(Anc);
			    rw.Cells.Add(c1);
			    Tb.Rows.Add(rw);
			    Controls.Add(Tb);
            }
            ChildControlsCreated        =   true;
        }

        protected override void OnPreRender(EventArgs e)
        {
            if ( PortalSettings.IsUnderCRM )
                Visible                 =   false;
            else Visible                =   true;
            base.OnPreRender(e);
        }
    }
}
